/*
 * 新增、添加测试用例
 * */
Ext.define('MobileTest.view.window.TestCaseWindow',{
	extend:'Ext.window.Window',
	width:800,
	height:650,
	modal:true,
 	title:'新建下发任务',
 	TestTaskCode:'',
 	act:'new',
	initComponent : function() {
		
		var me = this;
		
		var editfrm= Ext.create('Ext.form.Panel', {    			
    					bodyPadding: 5,
    					//width: 350,
    					region:'north',
    					layout: 'anchor',
    					defaults: {
        					anchor: '95%',        					
    					},
    					 fieldDefaults: {
            				labelAlign: 'right'            				
        				},
    					defaultType: 'textfield',
    					items: [{
    							xtype : 'fieldcontainer',
        		                defaultType: 'textfield',
        		                anchor:'100%',
        		                layout:'hbox',
        		            	fieldDefaults: {
            							labelAlign: 'right'            				
        						},
        		                items:[
        		                  		{
        									fieldLabel: '任务名称',
        									name: 'TestTaskName',
        									anchor:'50%',
        									allowBlank: false
    									},{
        									fieldLabel: '模版',
        									name: 'task_tree_node',
        									anchor:'50%'
    								}
        		                 ]},{
        							fieldLabel: '描述',
        							name: 'TestTaskDesc',
        							allowBlank: false
    							},{
        		                    		xtype : 'fieldcontainer',
        		                    		defaultType: 'textfield',
        		                    		anchor:'100%',
        		                    		layout:'hbox',
        		            			 	fieldDefaults: {
            									labelAlign: 'right'            				
        									},
        		                    		items:[{
        	        		                    fieldLabel: '日志级别',
        	        		                    allowBlank: false,
        	        		                    xtype:'combobox',
    									  		store: Ext.create('MobileTest.store.LogLevel'),
    									  		queryMode: 'local',
    									  		displayField: 'name',
    									  		valueField: 'value',
        	        		                    name: 'LogLevel'
        	        		                },{
        		                    xtype      : 'fieldcontainer',
        		                    fieldLabel : '抓包',
        		                    defaultType: 'radiofield',
        		                    defaults: {
        		                        flex: 1
        		                    },
        		                    layout: 'hbox',
        		                    items: [
        		                        {
        		                            boxLabel  : '需要',
        		                            name      : 'NetCapture',        		                            
        		                            inputValue: 1,
        		                            id        : 'NetCapture_radio1',
        		                            listeners:{"change":function( t,  newValue,  oldValue, eOpts){
        		                            	        		                            	
        		                            	
        		                            	
        		                            	
        		                            }}
        		                        }, {
        		                            boxLabel  : '不需要',
        		                            name      : 'NetCapture',
        		                            checked :   true,
        		                            inputValue: 0,
        		                            id        : 'NetCapture_radio2'
        		                        }
        		                    ]
        		                } ]
        		                    	},{
        		                    		xtype : 'fieldcontainer',
        		                    		defaultType: 'textfield',
        		                    		anchor:'100%',
        		                    		layout:'hbox',
        		                    		fieldDefaults: {
            									labelAlign: 'right'            				
        									},
        		                    		items:[{
        	        		                    fieldLabel: '类型',
        	        		                    allowBlank: false,
        	        		                    xtype:'combobox',
    											store: Ext.create('MobileTest.store.TestTaskType'),
    											queryMode: 'local',
    											displayField: 'name',
    											valueField: 'value',
        	        		                    name: 'TestTaskType'
        	        		                }, {
        	        		                    fieldLabel: '优先级',
        	        		                    allowBlank: false,
        	        		                    xtype:'combobox',
    											store: Ext.create('MobileTest.store.Priority'),
    											queryMode: 'local',
    											displayField: 'name',
    											valueField: 'value',
        	        		                    name: 'Priority'
        	        		                }]        		                    		
        		                    	},{
        		                    		xtype : 'fieldcontainer',
        		                    		defaultType: 'textfield',
        		                    		anchor:'100%',
        		                    		fieldDefaults: {
            									labelAlign: 'right'            				
        									},
        		                    		layout:'hbox',
        		                    		items:[{
    				fieldLabel : '开始时间',
    				xtype : 'fieldcontainer',    			
    				layout:'hbox',
    				items:[{
    					xtype : 'datefield',
    					name : 'txt_begin_date',
    					format : 'Y-m-d',
    					width:100,
    					value:new Date() ,
    					allowBlank: false 
    				},{
    					xtype : 'timefield',
    					name : 'txt_begin_time',
    					format : 'H:i',
    					value:'00:00',
    					width:60,
    					allowBlank: false
    				}]							
    			},{
    				fieldLabel : '结束时间',
    				xtype : 'fieldcontainer',    			
    				layout:'hbox',
    				items:[{
    					xtype : 'datefield',
    					name : 'txt_end_date',
    					format : 'Y-m-d',
    					width:100,
    					value:new Date() ,
    					allowBlank: false 
    				},{
    					xtype : 'timefield',
    					name : 'txt_end_time',
    					format : 'H:i',
    					width:60,
    					value:'23:59',
    					allowBlank: false
    				}]							
    			} ]        		                    		
        		                    	},	{
        		                xtype:'fieldset',
        		                title: '回捞清单生成规则配置',         		             
        		                columnWidth: 0.5,
        		                collapsible: true,
        		                defaultType: 'textfield',
        		                defaults: {anchor: '90%'},    
        		                collapsed: true,         		              
        		                layout:'anchor',
        		                items :[
        		                  {
        		                	  xtype : 'fieldcontainer',
        		                	  defaultType: 'textfield',
        		                	  anchor:'100%',
        		                	  fieldDefaults: {
            							labelAlign: 'right'            				
        					    	  },
        		                	  layout:'hbox',
        		                	  items:[{
        		                    xtype      : 'fieldcontainer',
        		                    fieldLabel : '执行类型',
        		                    defaultType: 'radiofield',
        		                    defaults: {
        		                        flex: 1
        		                    },
        		                    layout: 'hbox',
        		                    anchor:'100%',
        		                    items: [
        		                        {
        		                            boxLabel  : '按时执行',
        		                            name      : 'ExeType',        		                            
        		                            inputValue: 1,
        		                            id        : 'ExeType_radio1',
        		                            listeners:{"change":function( t,  newValue,  oldValue, eOpts){
        		                            	        		   
        		                            	
        		                            	
        		                            }}
        		                        }, {
        		                            boxLabel  : '按次执行',
        		                            name      : 'ExeType',
        		                            checked :   true,
        		                            inputValue: 2,
        		                            id        : 'ExeType_radio2'
        		                        }
        		                    ]
        		                },{
        		                		  fieldLabel: '重复类型',
        		                		  allowBlank: false,
        		                		  xtype:'combobox',
    									  store: Ext.create('MobileTest.store.IterationType'),
    									  queryMode: 'local',
    									  displayField: 'name',
    									  valueField: 'value',
        		                		  name: 'IterationType'
        		                	  }]
        		                  },
        		                  {
        		                	  xtype : 'fieldcontainer',
        		                	  defaultType: 'textfield',
        		                	  anchor:'100%',
        		                	  layout:'hbox',
        		                	  fieldDefaults: {
            							labelAlign: 'right'            				
        					    	  },
        		                	  items:[{
        		                		  fieldLabel: '开始时间段',
        		                		  allowBlank: false,
        		                		  xtype: 'timefield', 
        		                		  format:'H:i:s'    ,   		                		  
        		                		  name: 'ExeBeginTime'
        		                	  },{
        		                		  fieldLabel: '结束时间段',
        		                		  allowBlank: false,
        		                		  xtype: 'timefield',        		                		  
        		                		  format:'H:i:s'    ,
        		                		  name: 'ExeEndTime'
        		                	  }]
        		                  },
        		                  {
        		                	  xtype : 'fieldcontainer',
        		                	  defaultType: 'textfield',
        		                	  anchor:'100%',
        		                	  fieldDefaults: {
            							labelAlign: 'right'            				
        					    	  },
        		                	  layout:'hbox',
        		                	  items:[{
        		                		  fieldLabel: '循环次数',
        		                		  allowBlank: false,
        		                		  xtype: 'numberfield',
        		                		  minValue: 0,
        		                		  value:0,
        		                		  name: 'IterationNum'
        		                	  },{
        		                		  fieldLabel: '间隔时间',
        		                		  allowBlank: false,
        		                		  xtype: 'numberfield',
        		                		  minValue: 0,
        		                		  value:0,
        		                		  name: '_Interval'
        		                	  }]
        		                  }
        		                ]}        		                    	
        		                        ]}
    				);
		
			var gridstore=	Ext.create('Ext.data.Store', {    	
    			fields:[{name:'Id',type:'int'}, 'testTaskCode', 'serviceCode','testKeyCode','devId'],    	
    			proxy: {
        			type: 'ajax',
        			url : 'showKeySchema',
        			reader: {
            			type: 'json',
            			root: 'rows'
        			}        	
    			}
			});
			

	    	
		var grid=Ext.create('Ext.grid.Panel',{
					region:'center',
					title: '拨测指标',
    				store: gridstore,
    				columns: [
        				{ header: '业务',  dataIndex: 'serviceCode' },
        				{ header: '指标', dataIndex: 'testKeyCode', flex: 1 },
        				{ header: '设备号', dataIndex: 'devId' }
    				],
    				dockedItems : [ {
					xtype : 'toolbar',
					dock : 'top',
					items : [ {
						text : '添加',
						tooltip : '添加测试任务',
						iconCls : 'add',
						handler : function() {
							
							Ext.create('MobileTest.view.window.KeySchemaWindow',
							{
									task:me.TestTaskCode,
									listeners:{'beforedestroy':function(){
										
										gridstore.on('beforeload', function (store, options) {	      	        
	        								var extraParams={
	        									task:me.TestTaskCode		    
		    								};	        
	        								Ext.apply(store.proxy.extraParams, extraParams);	      
	    								});
	    
	    								gridstore.load();		
													
									}}}).show(	);

							}
						},{
							text : '删除',
							tooltip : '删除测试任务',						
							iconCls : 'del',
							handler : function() {
								
							}
						},'-' ]}
				        ,{
			        		xtype: 'pagingtoolbar',
			        		store: gridstore,   
			        		dock: 'bottom',
			        		displayInfo: true
			    		}
				    ]
		});
		
		Ext.applyIf(me, {
			layout:'border',  
			items:[editfrm,grid	],
			dockedItems:[ {
					xtype : 'toolbar',
					dock : 'top',
					items : [ {
							text : '保存',
							tooltip : '保存添加测试任务',
							iconCls : 'add',
							handler : function() {
								
								var form = me.down('form').getForm();
            					if (form.isValid()) {
            						
            						var temp= form.findField("txt_begin_date").value;
									var begin_date=Ext.Date.format(temp,'Ymd');
		
									temp=form.findField("txt_begin_time").getValue();
									var begin_time=Ext.Date.format(temp,'Hi');
		
									temp=form.findField("txt_end_date").getValue();		
									var end_date=Ext.Date.format(temp,'Ymd');
		
									temp=form.findField("txt_end_time").getValue();
									var end_time=Ext.Date.format(temp,'Hi');
            						
                					form.submit({
                						url: 'submitTask',
    									params: {
        									TestTaskCode: me.TestTaskCode,
        									StartDate:begin_date+begin_time,
        									EndDate:end_date+end_time,
        									act:me.act
    									},
                    					success: function(form, action) {
                       						Ext.Msg.alert('Success', action.result.msg);
                       						
                    					},
                    					failure: function(form, action) {
                        					Ext.Msg.alert('Failed', action.result.msg);
                    					}
                					});
            					}
            					
							}
						}, {
							text : '关闭',
							tooltip : '关闭窗口',
							iconCls : 'add',
							handler : function() {
									me.close();
							}
					}]
			}]
			}
		);
		
		me.callParent(arguments);
		
		if(me.TestTaskCode==''){
			
			me.getNextVal(me);
			me.act='new';
		}else{
			me.act='update';
		}			
	},
	getNextVal:function(me){
				
		Ext.Ajax.request({
    		url: 'nextVal',
    		method:'GET',
    		params: {
        		name: 'task'
    		},
    		success: function(response){
        		var text = response.responseText;        		
        		me.TestTaskCode=text;     
        		   		        		
    		}
		});
		
	}
});
 
 